package com.tianyu.inspection.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.service.api.IProjectService;
import com.tianyu.inspection.entity.Project;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.ProjectMapper;
import io.ocs.model.OcsSimpleModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description ProjectService
 * @Date 2021/1/6 - 15:12
 **********************************/

@Service
public class ProjectServiceImpl implements IProjectService {


    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    /**
     * 查询项目数量
     *
     * @param queryMo 查询参数
     * @return 项目数量
     */
    @Override
    public Integer selectCount(QueryMo queryMo) {
        return Math.toIntExact(projectMapper.selectCount(new QueryWrapper<Project>().allEq(queryMo.getEqMap())));
    }

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return ModelMap
     */
    @Override
    public IPage<Project> searchListPage(QueryMo queryMo) {
        QueryWrapper<Project> wrapper = PageModel.getModelWrapper(queryMo);
        wrapper.notIn("id",1);
        IPage<Project> page = projectMapper.selectPage(PageModel.getModelPage(queryMo), wrapper);
        page.getRecords().forEach((project -> {
            if (Objects.nonNull(project.getClientLogo())){
                project.setClientLogo(ocsSimpleModel.getObjectUrl(project.getClientLogo()));
            }
        }));
        return page;
    }


    /**
     * 通过ID等参数查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 单条记录结果
     */
    @Override
    public Project selectOne(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.get("id"))){
            return projectMapper.selectOne(new QueryWrapper<Project>().allEq(queryMo.getEqMap()));
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        Project project = objectMapper.convertValue(operateMo.getEntity(), Project.class);
        return 1 == projectMapper.insert(project);
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.get("id"))){
            return 1 == projectMapper.delete(new QueryWrapper<Project>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 更新参数集
     * @return ModelMap
     */
    @Override
    public boolean update(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        Project project = objectMapper.convertValue(operateMo.getEntity(), Project.class);
        return 1 == projectMapper.update(project,new QueryWrapper<Project>().allEq(operateMo.getEqMap()));
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态]
     * @return 是否操作成功
     */
    @Override
    public boolean logicallyDelete(QueryMo queryMo) {
        return 1 == projectMapper.update(
                new Project().setState(1),
                new QueryWrapper<Project>().allEq(queryMo.getEqMap())
        );
    }
}
